<?
	  layout("/inc/win_layout.html",{title:sysTitle}){
    ?>
<div class="col-lg-4">
	<div class="panel" style="height: 390px; overflow: auto;">
		<div class="panel-heading">字典项管理</div>
			
		<div class="panel-body">
			<div style="margin-top: -10px;margin-left: 10px;">
			<button style="height: 35px;width: 55px;" type="button" class="btn btn-blue btn-xs" onclick="qingling()"><h5>根目录</h5></button></div>
			<ul id="tree" class="ztree" style="width:300px;" overflow:auto;">
				
			</ul>
		</div>
		
	</div>
</div>
<?} ?>
<script>
	function getQueryString(name) { 
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
	var r = window.location.search.substr(1).match(reg); 
	if (r != null) return unescape(r[2]); return null; 
	} 
	var winName=getQueryString("winName");
	var dictId = window.parent.window[winName].getdictIdZTree();			//所属  字典类Id
	//树状结构
	$(function() {
		loadOneTree();
/* 		initTree(); */
	});
	/* function initTree() {
		$.ajax({
			type : "post",
			data : {
				"dictId" : dictId
			},
			url : "./findallDictItemZTree.do",
			dataType : "json",
			beforeSend : function(XMLHttpRequest) {
			},
			success : drawTree,
		});
	} */
	var sum;
	var sortsum;
	/* function drawTree(data) {
		var setting = {
			view : {
				dblClickExpand : false,
			},
			data : {
				simpleData : {
					enable : true,
				}
			},
			check : {
				enable : false
			},
			callback : {
				onClick : onClick
			},
		};
		var zNodes = eval(data);
		$.fn.zTree.init($("#tree"), setting, zNodes).expandAll(true);
		zTree = $.fn.zTree.getZTreeObj("tree");
		zTree.expandNode(zTree.getNodes()[0], true);//指定某节点折叠
		var zTreeResult = zTree.getNodes();
		sum=zTreeResult.length+1;
		
	} */
	
	var treeSetting = {
		async: {
			enable: true, url:"./findallDictItemZTreeAsy.do",
			dataType:"json",
			autoParam:["id","dictId","value","level"]
		},
		view: {
		dblClickExpand: true,
		selectedMulti: false,
		expandSpeed: ($.browser.msie && parseInt($.browser.version)<=6)?"":"fast"
		},
		data: {
		  simpleData: {
		    enable:true
		  }
		},
		callback: {
			onClick : onClick
		 /*  beforeExpand: beforeExpand */
		}
	};
	
	function loadOneTree(){
		$.ajax({
			type: "post",
			url: "loadOneTree.do",
			data: {
				"dictId": dictId
			},
			dataType: "json",
			success: function(data) {
				if(data[0].msg=="OK"){
					loadTree(data[0].data);
				}else{
					alert("请求失败");
				}
			},
			error : function(data) {
				// alert("请求失败");
			}
		});
	}
	
	function loadTree(rootNode){
		var zTreeObj = $.fn.zTree.init($("#tree"), treeSetting, rootNode);
		zTree = $.fn.zTree.getZTreeObj("tree");
		zTree.expandNode(zTree.getNodes()[0], false);//指定某节点折叠
		var zTreeResult = zTree.getNodes();
		sum=zTreeResult.length+1;
	}
	
	
	
	var saveid;
	var savename;
	function onClick(e, treeId, treeNode) {
		saveid = treeNode.id;		//获取点击  树形 ID
		savename=treeNode.name;  //获取点击 树形 name
		
		sortsum=treeNode.value;

		if(typeof treeNode.children == 'undefined'){
			sum = 1;
		}else{
			sum=treeNode.children.length+1;
		}
	}
	
	var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
	
	function qingling(){
		window.parent.window[winName].$("#dictitemId").val("0");
	    window.parent.window[winName].$("#dictitemName").val("根节点");
	    window.parent.window[winName].layer.tips("Ok", "#xuanze", {time: 5000});
	    var values;
	    if(sum<10){
	    	values="0"+sum;
	    }else{
	    	values=sum;
	    }
	    //window.parent.window[winName].$("#value").val(values);
	   // window.parent.window[winName].$("#sort").val(sum);
	    window.parent.window[winName].dictValue();
		parent.layer.close(index);
	}
	function queren(){
			if(saveid==null){
					window.parent.window[winName].$("#dictitemId").val("0");
				    window.parent.window[winName].$("#dictitemName").val("根节点");
				    var values;
				    if(sum<10){
				    	values="0"+sum;
				    }else{
				    	values=sum;
				    }
				    //window.parent.window[winName].$("#value").val(values);
				   // window.parent.window[winName].$("#sort").val(sum);
				    window.parent.window[winName].dictValue();
					parent.layer.close(index);
					
				}else{
					var values;
				    if(sum<10){
				    	values="0"+sum;
				    }else{
				    	values=sum;
				    }
					window.parent.window[winName].$("#dictitemId").val(saveid);
				    window.parent.window[winName].$("#dictitemName").val(savename);
				    window.parent.window[winName].$("#dictId").val(dictId);
				   	window.parent.window[winName].layer.tips("Ok", "#xuanze", {time: 5000});
				   	
				   	//window.parent.window[winName].$("#value").val(sortsum+"."+values);
				   // window.parent.window[winName].$("#sort").val(sum);
				    window.parent.window[winName].dictValue();
					parent.layer.close(index);
				}
	}
	
	
</script>